﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mongaba.Core.Listeners;
using System.Net;
using Mongaba.Core;
using System.Net.Sockets;
using Mongaba.Core.ServiceContracts;
using Mongaba.Core.DataContracts;
using System.ServiceModel;
using Mongaba.Core.Factory;
using System.Diagnostics;

namespace Mongaba
{
    public delegate void frmNetworkLookUpCompletedHandle(string address, string userName);
    
    public partial class frmNetworkLookUp : Form
    {
        public event frmNetworkLookUpCompletedHandle frmNetworkLookUpCompleted;

        public frmNetworkLookUp()
        {
            InitializeComponent();
        }

        private void NetworkLookUp_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
            }
            
            ResponseListener.Stop();

            

        }

       

        void p_ResponseReceivedEvent(object sender, EventArgs e)
        {
            
                foreach (IPAddress s in ResponseListener.GetResponseList())
                {
                    if (!groups.ContainsKey(s.ToString()))
                    {
                       try
                        {

                            IMongabaCoordinator ic = CoordinatorFactory.GetRemoteInstance(s.ToString());
                            groups.Add(s.ToString(), ic.getGroup());

                        }
                        finally
                        {
                            CoordinatorFactory.RemoveRemoteInstanceConnection(s.ToString());
                        }                                             

                    }
                }

                lstGroups.DataSource = groups.Select(x =>new {Id = x.Key, Name = x.Value.Name}).ToArray();
           

        }

        Dictionary<string, GroupContract> groups = new Dictionary<string, GroupContract>();

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if ((lstGroups.SelectedValue != null) && (lstGroups.SelectedValue.ToString() != "0"))
            {
                frmInsertName ins = new frmInsertName();
                ins.Show();
                ins.OnInsertedUserName += new InsertedUserNameHandle(ins_OnInsertedUserName);
            }
            
        }

        void ins_OnInsertedUserName(string name)
        {
            frmNetworkLookUpCompleted(lstGroups.SelectedValue.ToString(), name);
            this.Hide();
            ResponseListener.Stop();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
            ResponseListener.Stop();
        }

        private void updateList(object sender, EventArgs e)
        {
            if (!ResponseListener.IsListening)
            {              
                ResponseListener.ResponseReceivedEvent += new EventHandler(p_ResponseReceivedEvent);
                ResponseListener.Start();
            }
            groups.Clear();
            lstGroups.DataSource = new[] { new { Id = "0", Name = "Não há grupos conhecidos." } };
            ResponseListener.Refresh();
        }

        private void tsClose_Click(object sender, EventArgs e)
        {
            this.Hide();
            ResponseListener.Stop();
        }

        private void frmNetworkLookUp_Shown(object sender, EventArgs e)
        {
           
        }

        private void frmNetworkLookUp_Activated(object sender, EventArgs e)
        {
            lstGroups.DataSource = new[] { new { Id = "0", Name = "Não há grupos conhecidos." } };
            updateList(null, null);
        }
        


       

       

       
    }
}
